/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CSISL4pk;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;

//JFreeChartImports
/**
 *
 * @author Josh
 */
public class GenerateReportGUI  extends JInternalFrame {
        static int openFrameCount = 0;
    static final int xOffset = 60, yOffset = 60;
    private ResultSet rs;
    
    //initialize components 
    JPanel reportP = new JPanel();
    
    JCheckBox crimeType = new JCheckBox();
    JCheckBox caseNumber = new JCheckBox();
    JCheckBox locationName = new JCheckBox();
    JCheckBox startDate = new JCheckBox();
    JCheckBox endDate = new JCheckBox();
    JCheckBox suspectName = new JCheckBox();
    JCheckBox victimName = new JCheckBox();
    
    
    JTextField caseNumTF = new JTextField();
    JTextField vNameTF = new JTextField();
    JTextField sNameTF = new JTextField();
    JTextField startDateTfMM = new JTextField();
    JTextField startDateTfDD = new JTextField();
    JTextField startDateTfYYYY = new JTextField();
    JTextField endDateTfMM = new JTextField();
    JTextField endDateTfDD = new JTextField();
    JTextField endDateTfYYYY = new JTextField();
    
    JTextArea summaryTx = new JTextArea();
    
    String[] crimes = {"ARSON", "ASSAULT", "AUTO_THEFT", "BATTERY", "BURGLARY", "DISORDERLY CONDUCT", "DUI", "MURDER", "ROBBERY", "SEXUAL ASSAULT", "THEFT", "VANDALISM"};
    JComboBox crimeTypeBox = new JComboBox(crimes);
    String[] locations = { "Avenues", "Sugarhouse", "Liberty East", "Liberty West", "Trolley Square", "Central", "University", "Rose Park", "Glendale", "Foothill", "Capitol Hill", "Downtown" };
    JComboBox locationBox = new JComboBox(locations);
    JButton searchButton = new JButton("Build");
    JButton cancelButton = new JButton("Close Wnd.");//to back out and not submit to sql
    JButton printButton = new JButton("Print");
    
    public GenerateReportGUI(){
        super("Generate Report: " + (++openFrameCount), 
              false, //resizable
              false, //closable
              false, //maximizable
              true);//iconifiable

        //...Create the GUI and put it in the window...
        
        JLabel gr = new JLabel("Generate Report");
        gr.setFont(new Font("SansSerif",2,18));
        gr.setBounds(20, 20, 150, 45);
        add(gr);
        
        add(reportP);
        reportP.setLayout(null);
        
        //add components
        summaryTx.setBounds(360,85,500,250);
        reportP.add(summaryTx);
        
        printButton.setBounds(660, 22, 100, 30);
        reportP.add(printButton);

        JLabel startdateL = new JLabel();
        reportP.add(startdateL);
        startdateL.setText("Start Date:");
        startdateL.setBounds(135, 135, 100, 25);
        
        JLabel enddateL = new JLabel();
        reportP.add(enddateL);
        enddateL.setText("End Date:");
        enddateL.setBounds(137, 170, 100, 25);
        
        reportP.add(startDateTfMM);
        startDateTfMM.setBounds(195, 135, 30, 30);
        reportP.add(startDateTfDD);
        startDateTfDD.setBounds(230, 135, 30, 30);
        reportP.add(startDateTfYYYY);
        startDateTfYYYY.setBounds(265, 135, 50, 30);
        
        reportP.add(endDateTfMM);
        endDateTfMM.setBounds(195, 170, 30, 30);

        reportP.add(endDateTfDD);
        endDateTfDD.setBounds(230, 170, 30, 30);
        reportP.add(endDateTfYYYY);
        endDateTfYYYY.setBounds(265, 170, 50, 30);
        
        searchButton.setBounds(550, 22, 100, 30);
        searchButton.addActionListener(new searchButtonListener());
        reportP.add(searchButton);

        cancelButton.setBounds(770, 22, 110, 30);
        reportP.add(cancelButton);

        System.out.println("insideNewCase");
        setSize(900,400);//size of window

        //Set the window's location.
        setLocation(200,10);

    }
    
    //this method need to get the ammount of crimes that happened in each area
    //from startDate to endDate
            public int[] crimeCount(String startDate, String endDate)
        {
            int array[] = new int[12];
            try
            {                 
		String myDriver = "com.mysql.jdbc.Driver";
		String myUrl = "jdbc:mysql://www.db4free.net:3306/csisl";//www.freesql.org:3306/csisl";
		Class.forName(myDriver);

		//connect to DB
		Connection conn = DriverManager.getConnection(myUrl, "csisaltlake", "cs2450");

		//Report Numbers
		String caseInfo = "SELECT locale, count(locale) AS total_count FROM cases "
				+ "WHERE crimeDate between '"+startDate+"' and '"+endDate+"' GROUP BY locale";
                                
		// create the java statement
		Statement st = conn.createStatement();

		// execute the query
		rs = st.executeQuery(caseInfo);
		
		//get results and add to specific part of array
		while (rs.next())
		{
			String locale = rs.getString ("locale");
			int total_count = rs.getInt ("total_count");
			
			if(locale.equals("Avenues")) array[0] = total_count;
			else if(locale.equals("Sugarhouse")) array[1] = total_count;
			else if(locale.equals("Liberty Eastues")) array[2] = total_count;
			else if(locale.equals("Liberty West")) array[3] = total_count;
			else if(locale.equals("Trolley Square")) array[4] = total_count;
			else if(locale.equals("Central")) array[5] = total_count;
			else if(locale.equals("University")) array[6] = total_count;
			else if(locale.equals("Rose Park")) array[7] = total_count;
			else if(locale.equals("Glendale")) array[8] = total_count;
			else if(locale.equals("Foothill")) array[9] = total_count;
			else if(locale.equals("Capitol Hill")) array[10] = total_count;
			else if(locale.equals("Downtown")) array[11] = total_count;
		}

		st.close();
		
	} catch (Exception e)
	{
		System.out.println(e);
	}
	
	return array;
        }
    
        private class searchButtonListener implements ActionListener
        {

            public void actionPerformed(ActionEvent e)
            {
             String start = startDateTfYYYY.getText()+"-"+startDateTfMM.getText()+"-"+startDateTfDD.getText();
             String end = endDateTfYYYY.getText()+"-"+endDateTfMM.getText()+"-"+endDateTfDD.getText();
                System.out.println(start);
                System.out.println(end);
                
             int crimes[] = crimeCount(start, end);
             for(int count=0;count<crimes.length; count++)
             {
                 System.out.println(crimes[count]);
             }             
             
             PieChart chart = new PieChart("CSI Salt Lake", "Crimes in Salt Lake City", crimes);
             chart.pack();
             chart.setVisible(true);

            }
        }
}
